package cn.iocoder.yudao.module.design.controller.admin.designer;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.design.controller.admin.designer.vo.*;
import cn.iocoder.yudao.module.design.convert.designer.DesignerConvert;
import cn.iocoder.yudao.module.design.dal.dataobject.designer.DesignerDO;
import cn.iocoder.yudao.module.design.service.designer.DesignerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 认证设计师")
@RestController
@RequestMapping("/designer")
@Validated
public class DesignerController {

    @Resource
    private DesignerService erService;

    @PostMapping("/create")
    @Operation(summary ="创建认证设计师")
    @PreAuthorize("@ss.hasPermission('designer:create')")
    public CommonResult<Long> createer(@Valid @RequestBody DesignerCreateReqVO createReqVO) {
        return success(erService.createer(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary ="更新认证设计师")
    @PreAuthorize("@ss.hasPermission('designer:update')")
    public CommonResult<Boolean> updateer(@Valid @RequestBody DesignerUpdateReqVO updateReqVO) {
        erService.updateer(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary ="删除认证设计师")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('designer:delete')")
    public CommonResult<Boolean> deleteer(@RequestParam("id") Long id) {
        erService.deleteer(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary ="获得认证设计师")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('designer:query')")
    public CommonResult<DesignerRespVO> geter(@RequestParam("id") Long id) {
        DesignerDO er = erService.geter(id);
        return success(DesignerConvert.INSTANCE.convert(er));
    }

    @GetMapping("/list")
    @Operation(summary ="获得认证设计师列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('designer:query')")
    public CommonResult<List<DesignerRespVO>> geterList(@RequestParam("ids") Collection<Long> ids) {
        List<DesignerDO> list = erService.geterList(ids);
        return success(DesignerConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary ="获得认证设计师分页")
    @PreAuthorize("@ss.hasPermission('designer:query')")
    public CommonResult<PageResult<DesignerRespVO>> geterPage(@Valid DesignerPageReqVO pageVO) {
        PageResult<DesignerDO> pageResult = erService.geterPage(pageVO);
        return success(DesignerConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary ="导出认证设计师 Excel")
    @PreAuthorize("@ss.hasPermission('designer:export')")
    public void exporterExcel(@Valid DesignerExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<DesignerDO> list = erService.geterList(exportReqVO);
        // 导出 Excel
        List<DesignerExcelVO> datas = DesignerConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "认证设计师.xls", "数据", DesignerExcelVO.class, datas);
    }

}
